<template>
  <transition name="dialog">
    <section v-show="show" class="dialog">
        <div>
            <div>{{ title }}</div>
            <div>{{ message }}</div>
            <div>
                <p @click="cancel">取消</p>
                <p @click="confirm">确定</p>
            </div>
        </div>
    </section>
  </transition>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { DiaLogProps } from './DiaLog'
export default defineComponent({
    name: 'DiaLog',
    setup () {
        const show = ref<boolean>(false)
        const message = ref<string>('')
        const title = ref<string>('')
        let successCallback: any
        let cancelCallback: any
        let runSuccess = false
        const confirm = (): void => {
            runSuccess = true
            if (successCallback) {
                successCallback()
            }
            cancel()
        }
        const cancel = (): void => {
            if (!runSuccess && cancelCallback) {
                cancelCallback()
            }
            setTimeout(() => {
                show.value = false
                successCallback = null
                cancelCallback = null
                runSuccess = false
            },200)
        }
        const open = (props: DiaLogProps): void => {
            successCallback = props.successCallback
            cancelCallback = props.cancelCallback
            message.value = props.message
            title.value = props.title
            show.value = true
        }
        return {
            confirm,
            cancel,
            open,
            show,
            title,
            message
        }
    }
})
</script>

<style scoped>
.dialog {
  position: fixed;
  top: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  user-select: none;
  background-color: rgba(0,0,0,.7);
}
.dialog > div{
    background-color: #fff;
    border-radius: 16px;
    width: 320px;
}
.dialog > div > div:nth-child(1){
    padding-top: 26px;
    text-align: center;
    font-size: 16px;
    line-height: 24px;
}
.dialog > div > div:nth-child(2){
    color: #646566;
    flex: 1;
    max-height: 60vh;
    padding: 26px 24px;
    overflow-y: auto;
    font-size: 14px;
    line-height: 20px;
    white-space: pre-wrap;
    text-align: center;
    word-wrap: break-word;
    padding-top: 8px;
}
.dialog > div > div:nth-child(3){
    display: flex;
    align-items: center;
}
.dialog > div > div:nth-child(3) > p{
    width: 50%;
    text-align: center;
    height: 48px;
    line-height: 48px;
    font-size: 16px;
}
.dialog > div > div:nth-child(3) > p:nth-child(2){
    color: #ee0a24;
}
.dialog-enter-active {
  animation: fade .2s;
}

.dialog-leave-active {
  animation: fade .2s reverse;
}

@keyframes fade {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
</style>